#include "VStdAfx.h"
#include "VGame.h"
#include "VGameWindow.h"
#include "VSimpleEntity.h"
#include "VStaticMesh.h"

#include "VEngine.h"

VGame* g_Game;

VGame::VGame(void):m_GameWindow(NULL)
{
	Quit = FALSE;
	m_Light = NULL;
}


VGame::~VGame(void)
{
}

BOOL VGame::Init()
{
	VEngine* Engine = new VEngine;
	if (!Engine->InitEngine(this))
	{
		return FALSE;
	}





#if VPLATFORM_WIN32
	m_GameWindow = VNew VGameWindowWin(this);
	m_GameWindow->Resize(1280, 720, VWSM_WINDOWED);
#endif 

	if (!Engine->PostInit())
	{
		return FALSE;
	}

//	VEnv.Engine->a
	
	VEnv.World->LoadLevel("Levels/DemoLevel");

	

//	VSphereEntity* TestSphere = VNew VSphereEntity;
//	m_Scene->AddComponent(TestSphere);

	/*VStaticMeshComponent* StaticMesh = VNew VStaticMeshComponent;
	StaticMesh->LoadMesh("test2.mesh");
	m_Scene->AddComponent(StaticMesh);

	m_Light = m_Scene->CreateLight(VLT_OMNI);

	VMatrix LightWorldTransfrom;
	LightWorldTransfrom.SetTranslate(2.0f,10.0f,1.0f);
	m_Light->SetTransform(LightWorldTransfrom);

	m_Scene->AddLight(m_Light);*/
	/*VLight* Light = new VLightPoint;

	m_Scene->AddLight(Light);*/
	g_Game = this;
	return TRUE;
}

void VGame::Shutdown()
{
	VEnv.Engine->ShutdownEngine();
}

void VGame::Run()
{
	VASSERT(VEnv.Engine);

	if (m_GameWindow == NULL)
	{
		return;
	}

	while(!Quit)
	{
		VEnv.Engine->UpdateEngine();
		


		VSleep(3);
	}
}

void VGame::Update(float DeltaTime)
{
	m_GameWindow->ProcessWindowMessages();
	
}


void VGame::DoRender()
{
	if (m_GameWindow)
	{
		m_GameWindow->Draw(m_Scene);
	}
}

static VVector3 LightPos(2.0f,10.0f,4.0f);

void VGame::OnKeyDown(UINT Key)
{
	if (Key =='W')
	{
		LightPos.y += 1.0f;
	}else if (Key == 'S')
	{
		LightPos.y -= 1.0f;
	}else if (Key == 'A')
	{
		LightPos.x -= 1.0f;
	}else if (Key == 'D')
	{
		LightPos.x += 1.0f;
	}

	VMatrix LightWorldTransfrom;
	LightWorldTransfrom.SetTranslate(LightPos);
	if (m_Light)
	{
		//m_Light->SetTransform(LightWorldTransfrom);
	}
	

}

void VGame::OnKeyUp(UINT Key)
{

}